package com.seednia.foundation.framework.util;

import java.util.Map;
import java.util.NoSuchElementException;

public class MapUtils {

	public static Object getValue(Map<String, ?> map, String[] path) {
		Object retval = null;
		
		Map<String, ?> currMap = map;
		for (int i=0; i < path.length -1; i++) {
			String pathElement = path[i];
			if (!currMap.containsKey(pathElement))
				throw new NoSuchElementException("Key ["+pathElement+"] not found");
			currMap = (Map<String, ?>)currMap.get(pathElement);
		}
		retval = currMap.get(path[path.length-1]);
		
		return retval;
	}
	
	public static Object getValue(Map<String, ?> map, String pathStr, String delimiter) {
		String[] path = pathStr.split(delimiter);
		
		return MapUtils.getValue(map, path);
	}
}
